package com.cindata

import grails.transaction.Transactional

@Transactional
class AccountFlowStageService
{
    def initFlowStage(Account account, Workflow workflow)
    {
        def executionSequence = AccountFlowStage.findByAccount(account, [sort: 'executionSequence', order: 'desc'])?.executionSequence
        def workflowStages = WorkflowStage.findAllByWorkflow(workflow)
        workflowStages?.each {
            def accountFlowStage = new AccountFlowStage()
            accountFlowStage.executionSequence = executionSequence ? (executionSequence + it?.executionSequence) : it?.executionSequence
            accountFlowStage.stage = it?.stage
            accountFlowStage.canReject = it?.canReject
            accountFlowStage.layout = it?.layout
            accountFlowStage.account = account
            if (accountFlowStage.validate())
            {
                accountFlowStage.save flush: true
            }
            else
            {
                println accountFlowStage.errors
            }
        }
    }
}
